package com.cn.yz.service.sysauthority.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.cn.yz.common.authority.AcceptToken;
import com.cn.yz.common.exception.BusinessException;
import com.cn.yz.common.exception.ErrMessage;
import com.cn.yz.common.utils.EncryptUtil;
import com.cn.yz.service.sysauthority.config.SystemConfigBean;
import com.cn.yz.service.sysauthority.manager.DefaultTokenManager;
import com.cn.yz.service.sysauthority.mapper.SysUserMapper;
import com.cn.yz.service.sysauthority.model.SysUser;
import com.cn.yz.service.sysauthority.service.LoginService;

@Service
@EnableConfigurationProperties({SystemConfigBean.class})
public class LoginServiceImpl implements LoginService {
	
	private static final Logger LOG = LoggerFactory.getLogger(LoginServiceImpl.class);
	
	@Autowired
	SystemConfigBean systemConfigBean;
	
	@Autowired
	SysUserMapper sysUserMapper;
	
	@Autowired
	DefaultTokenManager defaultTokenManager;

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.LoginService#modPassword(com.cn.yz.service.sysauthority.model.SysUser)
	 */
	@Override
	public void modPassword(SysUser sysUser) {
		if(sysUser.getId() == null) {
			throw new BusinessException(ErrMessage.ID_MUST);
		}
		String passwordEncrypt = sysUser.getPassword();
		if(StringUtils.isEmpty(passwordEncrypt)) {
			throw new BusinessException(ErrMessage.PASSWORD_MUST);
		}
		
		String salt = EncryptUtil.getSalt(systemConfigBean.getSaltLength());
		sysUser.setSalt(salt);
		try {
			sysUser.setPassword(EncryptUtil.encryptSha1(passwordEncrypt, salt));
			int result = sysUserMapper.updateByPrimaryKeySelective(sysUser);
			if (result == 0) {
				throw new BusinessException(ErrMessage.RECORD_NON_EXISTENT);
			} 
		} catch (NoSuchAlgorithmException e) {
			LOG.error("LoginServiceImpl.modPassword ---->"+e.getMessage());
			throw new BusinessException(ErrMessage.PASSWORD_CANNOT_ENCRYPT);
		} catch (UnsupportedEncodingException e) {
			LOG.error("LoginServiceImpl.modPassword ---->"+e.getMessage());
			throw new BusinessException(ErrMessage.PASSWORD_CANNOT_ENCRYPT);
		}
		
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.LoginService#login(com.cn.yz.service.sysauthority.model.SysUser)
	 */
	@Override
	public void login(SysUser loginSysUser, HttpServletResponse response) {
		
		String account = loginSysUser.getAccount();
		if(StringUtils.isEmpty(account)) {
			throw new BusinessException(ErrMessage.ACCOUNT_MUST);
		}
		
		String passwordEncrypt = loginSysUser.getPassword();
		if(StringUtils.isEmpty(passwordEncrypt)) {
			throw new BusinessException(ErrMessage.PASSWORD_MUST);
		}
		
		SysUser sysUser = sysUserMapper.getSysUserByAccount(account);
		//验证账户是否存在
		if(sysUser == null){
			throw new BusinessException(ErrMessage.ACCOUNT_INVALID);
		}
		
		try {
			//验证密码是否正确
			String password = EncryptUtil.encryptSha1(passwordEncrypt, sysUser.getSalt());
			if (!sysUser.getPassword().equals(password)) {
				throw new BusinessException(ErrMessage.LOGIN_FAIL);
			}
		} catch (NoSuchAlgorithmException e) {
			LOG.error("LoginServiceImpl.login ---->"+e.getMessage());
			throw new BusinessException(ErrMessage.PASSWORD_CANNOT_ENCRYPT);
		} catch (UnsupportedEncodingException e) {
			LOG.error("LoginServiceImpl.login ---->"+e.getMessage());
			throw new BusinessException(ErrMessage.PASSWORD_CANNOT_ENCRYPT);
		}
		
		//修改最后登录时间
		sysUser.setLastLoginTime(new Date());
		sysUserMapper.updateByPrimaryKeySelective(sysUser);
		Map<String,Object>	map = new HashMap<String,Object>();
		map.put("id", sysUser.getId());
		map.put("account", sysUser.getAccount());
		defaultTokenManager.createToken(response, map);
	}

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.LoginService#logout(java.lang.String)
	 */
	@Override
	public void logout(String account) {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.LoginService#modPasswordByAccount(com.cn.yz.service.sysauthority.model.SysUser)
	 */
	@Override
	public void modPasswordByAccount(SysUser sysUser) {
		String account = sysUser.getAccount();
		if(StringUtils.isEmpty(account)) {
			throw new BusinessException(ErrMessage.ACCOUNT_MUST);
		}
		SysUser querySysUser = sysUserMapper.getSysUserByAccount(account);
		if(querySysUser == null) {
			throw new BusinessException(ErrMessage.ACCOUNT_INVALID);
		}
		sysUser.setId(querySysUser.getId());
		sysUser.setUpdateBy(querySysUser.getId());
		this.modPassword(sysUser);
	}

}
